package com.vitaf.cash.app.sdk.dec

import com.vitaf.cash.app.VitafCashApplication
import com.vitaf.cash.app.configs.VitafConfigs
import com.vitaf.cash.app.sdk.dec.util.GombeCreditUserDec1
import com.vitaf.cash.app.sdk.dec.util.GombeCreditUserDec2
import com.vitaf.cash.app.sdk.dec.util.GombeCreditUserDec3
import com.vitaf.cash.app.sdk.dec.util.GombeCreditUserDec4
import com.vitaf.cash.app.sdk.dec.util.GombeCreditUserDec5
import com.vitaf.cash.app.sdk.dec.util.GombeCreditUserDec6
import com.vitaf.cash.app.sdk.dec.util.GombeCreditUserDec7
import com.vitaf.cash.app.utils.shared.VitafSharedHp
import com.vitaf.cash.app.utils.shared.VitafSharedKey
import com.google.gson.Gson

class GombeCreditUserDecManager() {
    val context = VitafCashApplication.app
    val product = VitafConfigs.GOMBECREDIT_APPNAME
    val token = VitafSharedHp.takeGombeCreditSharedString(VitafSharedKey.GOMBECREDIT_TOKEN)

    private fun parameter1(path: String, fetchData: () -> String?): String? {
        if (!formatJson(path)) return null
        val content = fetchData()?:""

        // TODO: 加密操作
//        val encrypted: String = EKHAesTool.encryptDefault(content)
//        return encrypted
        return content
    }

    private fun parameter2(path: String, key: String, fetchData: () -> String?): String? {
        if (!formatJson(path)) return null
        val data = fetchData() ?: return null
        val params = commonParameter(path).apply { put(key, data) }
        val content = Gson().toJson(params).trim()

        // TODO: 加密操作
//        val encrypted: String = EKHAesTool.encryptDefault(content)
//        return encrypted
        return content
    }

    private fun formatJson(path: String) = listOf(product, token, path).all { it.isNotBlank() }

    private fun commonParameter(path: String) = hashMapOf<String, Any>(
        "path" to path.replace("/", ""),
        "token" to token,
        "product" to product
    )

    fun takeUserDec1(path: String) = parameter1(path) {
        GombeCreditUserDec1(context, product, token, path).takeUserDec1Data()
    }

    fun takeUserDec7(path: String) = parameter1(path) {
        GombeCreditUserDec7(context, product, token, path).takeUserDec7Data()
    }

    fun takeUserDec3(path: String) = parameter2(path, "deviceHardwareGzip") {
        GombeCreditUserDec3(context).takeUserDec3Data()
    }

    fun takeUserDec2(path: String, onResult: (String?) -> Unit) {
        GombeCreditUserDec2(context = context, onResult = { bd ->
            val result = parameter2(path, "deviceBatteryGzip") { bd }
            onResult.invoke(result)
        }).start()
    }

    fun takeUserDec5(path: String) = parameter2(path, "deviceWifiGzip") {
        GombeCreditUserDec5(context).takeUserDec5Data()
    }

    fun takeUserDec4(path: String) = parameter2(path, "deviceStorageGzip") {
        GombeCreditUserDec4(context).takeUserDec4Data()
    }

    fun takeUserDec6(path: String) = parameter2(path, "appListGzip") {
        GombeCreditUserDec6(context).takeUserDec6Data()
    }
}